package jmine.tec.script.persistent.driver;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Classe abstrata de parsers de parâmetros
 * 
 * @author lundberg
 * @param <T> tipo de scriptBean preenchido
 */
public abstract class AbstractScriptParameterParser<T extends ScriptBean> implements ScriptParameterParser<T> {
    private final Pattern pattern;

    /**
     * Construtor
     * 
     * @param regex regex
     */
    protected AbstractScriptParameterParser(String regex) {
        this.pattern = Pattern.compile(regex);
    }

    /**
     * {@inheritDoc}
     */
    public boolean parse(T bean, String parameter) {
        Matcher matcher = this.pattern.matcher(parameter);
        if (matcher.matches()) {
            this.doParse(bean, matcher);
            return true;
        } else {
            return false;
        }
    }

    /**
     * Recebe o matcher que teve sucesso e preenche o bean com o parâmetro
     * 
     * @param bean bean
     * @param matcher matcher
     */
    protected abstract void doParse(T bean, Matcher matcher);
}